package spring.yongongsystem.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.yongongsystem.POJO.dto.ProductPageQueryDTO;
import spring.yongongsystem.POJO.entity.Product;
import spring.yongongsystem.mapper.ProductMapper;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.service.ProductService;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 新增产品
     * @param product
     */
    @Override
    public void save(Product product) {
        productMapper.insert(product);
    }

    /**
     * 编辑产品
     * @param product
     */
    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);
    }

    /**
     * 产品分页查询
     * @param productPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(ProductPageQueryDTO productPageQueryDTO) {
        PageHelper.startPage(productPageQueryDTO.getPage(), productPageQueryDTO.getPageSize());
        Page<Product> page = productMapper.pageQuery(productPageQueryDTO);
        List<Product> records = page.getResult();
        return new PageResult(productMapper.count(), records);
    }
}
